package com.fy.repo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fy.core.dto.PageDto;
import com.fy.core.vo.ApiRest;
import com.fy.core.vo.PageResult;
import com.fy.repo.entity.Repo;
import com.fy.repo.mapper.RepoMapper;
import com.fy.repo.service.RepoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 题库 服务实现类
 * </p>
 *
 * @author bjt
 * @since 2023-06-12
 */
@Service
public class RepoServiceImpl extends ServiceImpl<RepoMapper, Repo> implements RepoService {

    @Override
    public PageResult<Repo> pageFind(PageDto<Repo> pageDto) {
        IPage<Repo> page=new Page<>(pageDto.getNow(),pageDto.getSize());
        Repo where=pageDto.getWhere();
        QueryWrapper<Repo> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(where.getTitle())){
            queryWrapper.lambda().like(Repo::getTitle,where.getTitle());
        }
        IPage<Repo> pageInfo=this.baseMapper.selectPage(page,queryWrapper);
        PageResult<Repo> pageResult=new PageResult<>();
        pageResult.setPages(pageInfo.getPages());
        pageResult.setList(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public ApiRest batchDelete(List<Repo> repos) {
        List<String> ids=repos.stream().map(item->item.getId()).collect(Collectors.toList());
        this.baseMapper.deleteBatchIds(ids);
        return ApiRest.success("批量删除成功");
    }
}
